package com.wdhcr.osspolicy.controller;

import com.wdhcr.osspolicy.component.OssComponent;
import com.wdhcr.osspolicy.mode.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @description: aliyun oss前端上传后回调地址
 * @author: wdhcr
 * @dateTime: 2021/3/7 19:55
 */
@RestController
@RequestMapping("/ali/oss")
public class OssCallbackController {

    @Autowired
    private OssComponent ossComponent;

    @PostMapping("/callback")
    public R ossCallback(HttpServletRequest request) throws IOException {
//        request 只能获取一次
        String body = ossComponent.GetPostBody(request.getInputStream(), Integer.parseInt(request.getHeader("content-length")));
        String[] strings = body.split("&");
        String filename = strings[0].split("=")[1];
        boolean b = ossComponent.VerifyOSSCallbackRequest(request,body);
        if (b) {
            String url = ossComponent.getUrl(filename);
            return R.success(url);
        }else {
            return R.error("上传失败");
        }
    }
}
